package org.example.springmvc.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

@RequestMapping("pt")
@RestController
public class Test_1_12 {
    @RequestMapping("test1")
    public String test1(String name) {
        return "接收到的参数名为：" + name;
    }
    @RequestMapping("test2")
    public String test2(Integer year) {
        return "接收到的参数名为：" + year;
    }
    @RequestMapping("test3")
    public String test3(int year) {
        return "接收到的参数名为：" + year;
    }
    @RequestMapping("test4")
    public String test4(String name,Integer year) {
        return "接收到的参数名为：" + name + " " + year;
    }
    @RequestMapping("test5")
    public String test5(Person person) {
        return "接收到的参数名为：" + person.toString();
    }
    @RequestMapping("test6")
    public String test6(@RequestParam(value = "time",required = false) String createtime) {
        return "接收到的参数名为：" + createtime;
    }
    @RequestMapping("test7")
    public String test7(String[] names) {
        return "接收到的参数名为：" + Arrays.toString(names);
    }
    @RequestMapping("test8")
    public String test8(@RequestParam(required = false) ArrayList<String> names) {
        return "接收到的参数名为：" + names.toString();
    }
    @RequestMapping("test9")
    public String test9(@RequestBody Person person) {
        return "接收到的参数名为：" + person.toString();
    }
    @RequestMapping("/test10/{id}/{name}")
    public String test10(@PathVariable Integer id, @PathVariable("name") String userName){
        return "解析参数id:"+id+",name:"+userName;
    }
    @RequestMapping("/test11")
    public String getfile(@RequestPart("file") MultipartFile file) throws IOException {
        //获取⽂件名称
        String fileName = file.getOriginalFilename();
        //⽂件上传到指定路径
        file.transferTo(new File("D:/temp/" + file.getOriginalFilename()));
        return "接收到⽂件名称为: "+fileName;
    }
    @RequestMapping("/test12")
    public String test12(HttpServletRequest request, HttpServletResponse response) {
        // 获取所有 cookie 信息
        Cookie[] cookies = request.getCookies();
        //打印Cookie信息
        StringBuilder builder = new StringBuilder();
        if (cookies!=null){
            for (Cookie ck:cookies) {
                builder.append(ck.getName()+":"+ck.getValue());
            }
        }
        return "Cookie信息:"+builder;
    }
    @RequestMapping("/test13")
    public String test13(@CookieValue String name) {
        return "name：" + name;
    }
    @RequestMapping("/test14")
    public String test14(HttpServletRequest request) {
        // 获取Session对象
        HttpSession session = request.getSession();
        if (session != null) {
            session.setAttribute("username", "java");
        }
        return "session 存储成功";
    }
    @RequestMapping("/test15")
    public String test15(HttpServletRequest request) {
        // 如果 session 不存在, 不会⾃动创建
        HttpSession session = request.getSession(false);
        String username = null;
        if (session != null && session.getAttribute("username") != null) {
            username = (String) session.getAttribute("username");
        }
        return "username：" + username;
    }
    @RequestMapping("/test16")
    public String test16(@SessionAttribute(value = "username",required = false) String username){
        return "username："+username;
    }
    @RequestMapping("/test17")
    public String test17(HttpServletRequest request, HttpServletResponse response) {
        String userAgent = request.getHeader("User-Agent");
        return  "userAgent："+userAgent;
    }
    @RequestMapping("/test18")
    public String test18(@RequestHeader("User-Agent") String userAgent) {
        return "userAgent："+userAgent;
    }
}
